package com.csi.controller.user;

import com.alibaba.druid.util.StringUtils;
import com.alibaba.fastjson2.JSON;
import com.csi.pojo.Result;
import com.csi.service.UserService;
import com.csi.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet("/user/updatePayLimitController")
public class UpdatePayLimitController extends HttpServlet {

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        resp.setContentType("application/json;charset=utf-8");
        PrintWriter out = resp.getWriter();

        // 获取请求参数
        String userNumber = req.getParameter("userNumber");
        String payLimitStr = req.getParameter("payLimit");
        String userIdStr = req.getParameter("userId");
        String payPassword = req.getParameter("payPassword"); // 输入的支付密码

        // 参数校验
        if (StringUtils.isEmpty(userNumber) || StringUtils.isEmpty(payLimitStr) || StringUtils.isEmpty(userIdStr) || StringUtils.isEmpty(payPassword)) {
            Result result = new Result<>();
            result.setCode(400);
            result.setMsg("请求参数不能为空");
            out.print(JSON.toJSONString(result));
            out.flush();
            out.close();
            return;
        }

        Double payLimit = null;
        Integer userId = null;
        try {
            payLimit = Double.valueOf(payLimitStr);
            userId = Integer.valueOf(userIdStr);
        } catch (NumberFormatException e) {
            Result result = new Result<>();
            result.setCode(400);
            result.setMsg("请求参数格式不正确");
            out.print(JSON.toJSONString(result));
            out.flush();
            out.close();
            return;
        }

        UserService userService = new UserServiceImpl();

        try {
            // 查找支付密码，验证是否为空
            String realPayPassword = userService.queryPayPassword(userNumber);
            if (realPayPassword == null) {
                Result result = new Result<>();
                result.setCode(7001);
                result.setMsg("支付密码为空");
                out.print(JSON.toJSONString(result));
                return;
            }

            // 验证支付密码
            boolean flag = userService.checkPayPassword(userNumber, payPassword, userId);
            if (!flag) {
                Result result = new Result<>();
                result.setCode(400);
                result.setMsg("支付密码错误");
                out.print(JSON.toJSONString(result));
                return;
            }

            // 更新支付限额
            userService.updatePayLimit(userNumber, payLimit);

            Result result = new Result<>();
            result.setCode(200);
            result.setMsg("更新成功");
            out.print(JSON.toJSONString(result));
        } catch (Exception e) {
            Result result = new Result<>();
            result.setCode(500);
            result.setMsg("服务器内部错误");
            out.print(JSON.toJSONString(result));
            e.printStackTrace();
        } finally {
            out.flush();
            out.close();
        }
    }

}
